﻿create PROCEDURE Sync.SP_CommitteeBankAccountDepositReceipts_ApplyChanges
@RawValues XML 
AS 
BEGIN
	 
		DECLARE @CommitteeBankAccountDepositReceipts_Temp TABLE 
		(
			CommitteeBankAccountDepositReceiptID UniqueIdentifier,
			[CommitteeBankAccountDepositID] uniqueidentifier,
				[ReceiptID] uniqueidentifier,
			IsDeleted BIT, 
			LastUpdatedDate DATETIME
		)
		
	   INSERT INTO @CommitteeBankAccountDepositReceipts_Temp 
	   SELECT	Tbl.Col.value('CommitteeBankAccountDepositReceiptID[1]','uniqueidentifier') CommitteeBankAccountDepositReceiptID,
                Tbl.Col.value('CommitteeBankAccountDepositID[1]', 'uniqueidentifier') [CommitteeBankAccountDepositID],
Tbl.Col.value('ReceiptID[1]', 'uniqueidentifier') [ReceiptID],
				Tbl.Col.value('IsDeleted[1]', 'bit') IsDeleted,
				Tbl.Col.value('LastUpdatedDate[1]','DateTime') LastUpdatedDate
	   FROM   @RawValues.nodes('/DocumentElement/CommitteeBankAccountDepositReceipts') Tbl(Col)
----------------------------------------------------------------------------------------

-- Insert ------------------------------------------------------------------------------

	INSERT INTO [dbo].[CommitteeBankAccountDepositReceipts] ([CommitteeBankAccountDepositReceiptID],[CommitteeBankAccountDepositID],[ReceiptID])
	SELECT t.[CommitteeBankAccountDepositReceiptID],t.[CommitteeBankAccountDepositID],t.[ReceiptID]
	FROM  @CommitteeBankAccountDepositReceipts_Temp t
	WHERE t.IsDeleted = 0
    AND NOT EXISTS
    (
        SELECT 1 FROM sync.CommitteeBankAccountDepositReceipts_Tracking s
        WHERE t.CommitteeBankAccountDepositReceiptID = s.CommitteeBankAccountDepositReceiptID
    )
	-- put conflicts here.
	
-- Update -------------------------------------------------------------------------------
     
	UPDATE [dbo].[CommitteeBankAccountDepositReceipts]
	SET [CommitteeBankAccountDepositReceipts].[CommitteeBankAccountDepositID] = t.[CommitteeBankAccountDepositID],[CommitteeBankAccountDepositReceipts].[ReceiptID] = t.[ReceiptID] 
	FROM [dbo].[CommitteeBankAccountDepositReceipts] s JOIN @CommitteeBankAccountDepositReceipts_Temp t 
    ON t.CommitteeBankAccountDepositReceiptID = s.CommitteeBankAccountDepositReceiptID
	WHERE	t.IsDeleted = 0 
	AND		t.LastUpdatedDate IS NOT NULL
	AND		t.LastUpdatedDate >= 
				 isnull((SELECT TOP 1 LastUpdatedDate 
				  FROM sync.CommitteeBankAccountDepositReceipts_Tracking r
				  WHERE t.CommitteeBankAccountDepositReceiptID = r.CommitteeBankAccountDepositReceiptID)
                  ,t.LastUpdatedDate)
	-----------------------------------------------------------------------------------------

-- Delete -------------------------------------------------------------------------------

	DELETE [dbo].[CommitteeBankAccountDepositReceipts]
    FROM @CommitteeBankAccountDepositReceipts_Temp t JOIN [dbo].[CommitteeBankAccountDepositReceipts] s
    ON t.CommitteeBankAccountDepositReceiptID = s.CommitteeBankAccountDepositReceiptID
	WHERE t.IsDeleted = 1

-----------------------------------------------------------------------------------------
	
END





